package edu.ayit.service.impl;

import edu.ayit.dao.CommentRepository;
import edu.ayit.pojo.Comment;
import edu.ayit.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;

    /**
     * 获取有层次结构的评论,为了实现这一功能，定义三个辅助方法：
     * 1.eachComment(List<Comment> comments)
     * 2.combineChildren(List<Comment> comments)
     * 3.recursively(Comment comment)
     */
    @Override
    public List<Comment> getCommentListById(Long blogId) {
        Sort sort = Sort.by(Sort.Direction.ASC,"createTime");
        //获取顶级评论集合（父评论为空的评论）
        List<Comment> comments = commentRepository.findAllByBlogIdAndParentCommentNull(blogId, sort);
        System.out.println("---------------------顶级---------------------");
        return this.eachComment(comments);
    }

    /**
     * 遍历顶级评论集合
     */
    private List<Comment> eachComment(List<Comment> comments){
        List<Comment> comments2 = new ArrayList<>();
        for (Comment comment : comments) {//复制数据库中的评论信息，避免修改数据库中的信息
            Comment c = new Comment();
            BeanUtils.copyProperties(comment,c);
            comments2.add(c);
        }
        //这个方法的作用是合并顶级评论的各层子代到第一级子代集合中
        this.combineChildren(comments2);
        return comments2;
    }
    // 存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();
    private void combineChildren(List<Comment> comments){
        for (Comment comment : comments) {
            List<Comment> replyComments = comment.getReplyComments();
            for (Comment replyComment : replyComments) {
                //这个方法的作用是循环迭代找出所有子代，并存放在tempReplys中
                this.recursively(replyComment);
            }
            System.out.println("--------------------------------------------------------------------");
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setReplyComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
    }
    /**
     * 递归迭代，剥洋葱
     */
    private void recursively(Comment comment){
        tempReplys.add(comment);//把顶级节点的一级子评论添加到临时集合
        if(comment.getReplyComments().size()>0){
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size()>0) {
                    recursively(reply);
                }
            }
        }
    }



    @Transactional
    @Override
    public Comment saveComment(Comment comment) {
        Long  parentCommentId = comment.getParentComment().getId();
        if(parentCommentId!=-1){//有父评论
            comment.setParentComment(commentRepository.getOne(parentCommentId));
        }else {
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentRepository.save(comment);
    }
}
